package cz.muni.fi.pa165.web.validation;

import cz.muni.fi.pa165.api.dto.ForestDTO;
import cz.muni.fi.pa165.api.dto.ForestVisitDTO;
import org.springframework.stereotype.Service;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 * Validation class for ForestVisitDTO
 *
 * @author Matej Cimbora
 */
@Service
public class ForestVisitDTOValidator implements Validator {

    @Override
    public boolean supports(Class<?> type) {
        return ForestDTO.class.equals(type);
    }

    @Override
    public void validate(Object o, Errors errors) {
        ForestVisitDTO forestVisit = (ForestVisitDTO) o;
        if (forestVisit.getForestDTO() == null) {
            errors.rejectValue("forestDTO", "forestVisit.forest.empty");
        }
        if (forestVisit.getPickerDTO() == null) {
            errors.rejectValue("pickerDTO", "forestVisit.picker.empty");
        }
        if (forestVisit.getMushroomsFound() == null) {
            errors.rejectValue("mushroomsFound", "forestVisit.mushroomsFound.empty");
        }
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dateOfVisit", "forestVisit.date.empty");
    }
}
